
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 07/18/2014 00:38:14
-- Generated from EDMX file: D:\Sources\Mapas\trunk\MapGeoData\Data\BaseModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [geodata_db];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CompanyGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Groups] DROP CONSTRAINT [FK_CompanyGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_UserCompanyGroup_UserCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanyGroup] DROP CONSTRAINT [FK_UserCompanyGroup_UserCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_UserCompanyGroup_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanyGroup] DROP CONSTRAINT [FK_UserCompanyGroup_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_RolUserCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanies] DROP CONSTRAINT [FK_RolUserCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_UserUserCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanies] DROP CONSTRAINT [FK_UserUserCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_UserCompanyUserPreference]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserPreferences] DROP CONSTRAINT [FK_UserCompanyUserPreference];
GO
IF OBJECT_ID(N'[dbo].[FK_PreferenceUserPreference]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserPreferences] DROP CONSTRAINT [FK_PreferenceUserPreference];
GO
IF OBJECT_ID(N'[dbo].[FK_PreferenceCompanyPreference]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CompanyPreferences] DROP CONSTRAINT [FK_PreferenceCompanyPreference];
GO
IF OBJECT_ID(N'[dbo].[FK_CompanyCompanyPreference]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CompanyPreferences] DROP CONSTRAINT [FK_CompanyCompanyPreference];
GO
IF OBJECT_ID(N'[dbo].[FK_CompanyUserCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanies] DROP CONSTRAINT [FK_CompanyUserCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_CompanyRol]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Rols] DROP CONSTRAINT [FK_CompanyRol];
GO
IF OBJECT_ID(N'[dbo].[FK_ProcessRol_Process]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProcessRol] DROP CONSTRAINT [FK_ProcessRol_Process];
GO
IF OBJECT_ID(N'[dbo].[FK_ProcessRol_Rol]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProcessRol] DROP CONSTRAINT [FK_ProcessRol_Rol];
GO
IF OBJECT_ID(N'[dbo].[FK_GeoTypeGeoData]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GeoDatas] DROP CONSTRAINT [FK_GeoTypeGeoData];
GO
IF OBJECT_ID(N'[dbo].[FK_SolutionProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects] DROP CONSTRAINT [FK_SolutionProject];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectGeoData]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GeoDatas] DROP CONSTRAINT [FK_ProjectGeoData];
GO
IF OBJECT_ID(N'[dbo].[FK_SolutionSharedSolution]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSolutions] DROP CONSTRAINT [FK_SolutionSharedSolution];
GO
IF OBJECT_ID(N'[dbo].[FK_PermissionSharedSolution]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSolutions] DROP CONSTRAINT [FK_PermissionSharedSolution];
GO
IF OBJECT_ID(N'[dbo].[FK_UserCompanyUserSolution_UserCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanyUserSolution] DROP CONSTRAINT [FK_UserCompanyUserSolution_UserCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_UserCompanyUserSolution_UserSolution]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserCompanyUserSolution] DROP CONSTRAINT [FK_UserCompanyUserSolution_UserSolution];
GO
IF OBJECT_ID(N'[dbo].[FK_GeoDataBoobleData]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BoobleDatas] DROP CONSTRAINT [FK_GeoDataBoobleData];
GO
IF OBJECT_ID(N'[dbo].[FK_StyleGeoData]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GeoDatas] DROP CONSTRAINT [FK_StyleGeoData];
GO
IF OBJECT_ID(N'[dbo].[FK_ProcessGroupProcess]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Processes] DROP CONSTRAINT [FK_ProcessGroupProcess];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[UserPreferences]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserPreferences];
GO
IF OBJECT_ID(N'[dbo].[Preferences]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Preferences];
GO
IF OBJECT_ID(N'[dbo].[Companies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Companies];
GO
IF OBJECT_ID(N'[dbo].[UserCompanies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserCompanies];
GO
IF OBJECT_ID(N'[dbo].[Groups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Groups];
GO
IF OBJECT_ID(N'[dbo].[Rols]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Rols];
GO
IF OBJECT_ID(N'[dbo].[Processes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Processes];
GO
IF OBJECT_ID(N'[dbo].[CompanyPreferences]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CompanyPreferences];
GO
IF OBJECT_ID(N'[dbo].[ProcessGroups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProcessGroups];
GO
IF OBJECT_ID(N'[dbo].[Solutions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Solutions];
GO
IF OBJECT_ID(N'[dbo].[Projects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Projects];
GO
IF OBJECT_ID(N'[dbo].[GeoDatas]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GeoDatas];
GO
IF OBJECT_ID(N'[dbo].[UserSolutions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserSolutions];
GO
IF OBJECT_ID(N'[dbo].[GeoTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GeoTypes];
GO
IF OBJECT_ID(N'[dbo].[Permissions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Permissions];
GO
IF OBJECT_ID(N'[dbo].[BoobleDatas]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BoobleDatas];
GO
IF OBJECT_ID(N'[dbo].[Styles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Styles];
GO
IF OBJECT_ID(N'[dbo].[UserCompanyGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserCompanyGroup];
GO
IF OBJECT_ID(N'[dbo].[ProcessRol]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProcessRol];
GO
IF OBJECT_ID(N'[dbo].[UserCompanyUserSolution]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserCompanyUserSolution];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [UserId] int IDENTITY(1,1) NOT NULL,
    [FullName] nvarchar(255)  NOT NULL,
    [Login] nvarchar(100)  NOT NULL,
    [Password] nvarchar(255)  NOT NULL,
    [Email] nvarchar(255)  NOT NULL,
    [System] bit  NOT NULL,
    [Active] bit  NOT NULL
);
GO

-- Creating table 'UserPreferences'
CREATE TABLE [dbo].[UserPreferences] (
    [UserPreferenceId] int IDENTITY(1,1) NOT NULL,
    [UserCompanyId] int  NOT NULL,
    [PreferenceId] int  NOT NULL,
    [Value] nvarchar(500)  NOT NULL
);
GO

-- Creating table 'Preferences'
CREATE TABLE [dbo].[Preferences] (
    [PreferenceId] int IDENTITY(1,1) NOT NULL,
    [Code] nvarchar(50)  NOT NULL,
    [Description] nvarchar(255)  NULL
);
GO

-- Creating table 'Companies'
CREATE TABLE [dbo].[Companies] (
    [CompanyId] int IDENTITY(1,1) NOT NULL,
    [Code] nvarchar(50)  NOT NULL,
    [Name] nvarchar(255)  NULL,
    [System] bit  NOT NULL
);
GO

-- Creating table 'UserCompanies'
CREATE TABLE [dbo].[UserCompanies] (
    [UserCompanyId] int IDENTITY(1,1) NOT NULL,
    [UserId] int  NOT NULL,
    [RolId] int  NOT NULL,
    [CompanyId] int  NOT NULL
);
GO

-- Creating table 'Groups'
CREATE TABLE [dbo].[Groups] (
    [GroupId] int IDENTITY(1,1) NOT NULL,
    [CompanyId] int  NOT NULL,
    [Code] nvarchar(50)  NOT NULL,
    [Name] nvarchar(255)  NULL,
    [System] bit  NOT NULL
);
GO

-- Creating table 'Rols'
CREATE TABLE [dbo].[Rols] (
    [RolId] int IDENTITY(1,1) NOT NULL,
    [CompanyId] int  NOT NULL,
    [Code] nvarchar(50)  NOT NULL,
    [Name] nvarchar(255)  NULL,
    [System] bit  NOT NULL
);
GO

-- Creating table 'Processes'
CREATE TABLE [dbo].[Processes] (
    [Code] nvarchar(50)  NOT NULL,
    [Name] nvarchar(255)  NULL,
    [GroupCode] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'CompanyPreferences'
CREATE TABLE [dbo].[CompanyPreferences] (
    [CompanyPreferenceId] int IDENTITY(1,1) NOT NULL,
    [PreferenceId] int  NOT NULL,
    [CompanyId] int  NOT NULL,
    [Value] nvarchar(500)  NOT NULL
);
GO

-- Creating table 'ProcessGroups'
CREATE TABLE [dbo].[ProcessGroups] (
    [Code] nvarchar(50)  NOT NULL,
    [Name] nvarchar(255)  NULL
);
GO

-- Creating table 'Solutions'
CREATE TABLE [dbo].[Solutions] (
    [SolutionId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(255)  NOT NULL,
    [Description] nvarchar(255)  NULL,
    [Deleted] bit  NOT NULL
);
GO

-- Creating table 'Projects'
CREATE TABLE [dbo].[Projects] (
    [ProjectId] int IDENTITY(1,1) NOT NULL,
    [SolutionId] int  NOT NULL,
    [Name] nvarchar(255)  NOT NULL,
    [Description] nvarchar(255)  NULL,
    [Deleted] bit  NOT NULL
);
GO

-- Creating table 'GeoDatas'
CREATE TABLE [dbo].[GeoDatas] (
    [GeoDataId] int IDENTITY(1,1) NOT NULL,
    [GeoTypeId] int  NOT NULL,
    [ProjectId] int  NOT NULL,
    [Data] geography  NOT NULL,
    [StyleId] int  NOT NULL
);
GO

-- Creating table 'UserSolutions'
CREATE TABLE [dbo].[UserSolutions] (
    [UserSolutionId] int IDENTITY(1,1) NOT NULL,
    [SolutionId] int  NOT NULL,
    [PermissionId] int  NOT NULL
);
GO

-- Creating table 'GeoTypes'
CREATE TABLE [dbo].[GeoTypes] (
    [GeoTypeId] int IDENTITY(1,1) NOT NULL,
    [Code] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Permissions'
CREATE TABLE [dbo].[Permissions] (
    [PermissionId] int IDENTITY(1,1) NOT NULL,
    [Code] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'BoobleDatas'
CREATE TABLE [dbo].[BoobleDatas] (
    [BoobleDataId] int IDENTITY(1,1) NOT NULL,
    [GeoDataId] int  NOT NULL,
    [Data] nvarchar(max)  NULL
);
GO

-- Creating table 'Styles'
CREATE TABLE [dbo].[Styles] (
    [StyleId] int IDENTITY(1,1) NOT NULL,
    [Code] nvarchar(50)  NOT NULL,
    [Data] nvarchar(max)  NULL
);
GO

-- Creating table 'UserCompanyGroup'
CREATE TABLE [dbo].[UserCompanyGroup] (
    [UserCompanyGroup_Group_UserCompanyId] int  NOT NULL,
    [Groups_GroupId] int  NOT NULL
);
GO

-- Creating table 'ProcessRol'
CREATE TABLE [dbo].[ProcessRol] (
    [Processes_Code] nvarchar(50)  NOT NULL,
    [ProcessRol_Process_RolId] int  NOT NULL
);
GO

-- Creating table 'UserCompanyUserSolution'
CREATE TABLE [dbo].[UserCompanyUserSolution] (
    [UserCompanies_UserCompanyId] int  NOT NULL,
    [UserSolutions_UserSolutionId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [UserId] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserPreferenceId] in table 'UserPreferences'
ALTER TABLE [dbo].[UserPreferences]
ADD CONSTRAINT [PK_UserPreferences]
    PRIMARY KEY CLUSTERED ([UserPreferenceId] ASC);
GO

-- Creating primary key on [PreferenceId] in table 'Preferences'
ALTER TABLE [dbo].[Preferences]
ADD CONSTRAINT [PK_Preferences]
    PRIMARY KEY CLUSTERED ([PreferenceId] ASC);
GO

-- Creating primary key on [CompanyId] in table 'Companies'
ALTER TABLE [dbo].[Companies]
ADD CONSTRAINT [PK_Companies]
    PRIMARY KEY CLUSTERED ([CompanyId] ASC);
GO

-- Creating primary key on [UserCompanyId] in table 'UserCompanies'
ALTER TABLE [dbo].[UserCompanies]
ADD CONSTRAINT [PK_UserCompanies]
    PRIMARY KEY CLUSTERED ([UserCompanyId] ASC);
GO

-- Creating primary key on [GroupId] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [PK_Groups]
    PRIMARY KEY CLUSTERED ([GroupId] ASC);
GO

-- Creating primary key on [RolId] in table 'Rols'
ALTER TABLE [dbo].[Rols]
ADD CONSTRAINT [PK_Rols]
    PRIMARY KEY CLUSTERED ([RolId] ASC);
GO

-- Creating primary key on [Code] in table 'Processes'
ALTER TABLE [dbo].[Processes]
ADD CONSTRAINT [PK_Processes]
    PRIMARY KEY CLUSTERED ([Code] ASC);
GO

-- Creating primary key on [CompanyPreferenceId] in table 'CompanyPreferences'
ALTER TABLE [dbo].[CompanyPreferences]
ADD CONSTRAINT [PK_CompanyPreferences]
    PRIMARY KEY CLUSTERED ([CompanyPreferenceId] ASC);
GO

-- Creating primary key on [Code] in table 'ProcessGroups'
ALTER TABLE [dbo].[ProcessGroups]
ADD CONSTRAINT [PK_ProcessGroups]
    PRIMARY KEY CLUSTERED ([Code] ASC);
GO

-- Creating primary key on [SolutionId] in table 'Solutions'
ALTER TABLE [dbo].[Solutions]
ADD CONSTRAINT [PK_Solutions]
    PRIMARY KEY CLUSTERED ([SolutionId] ASC);
GO

-- Creating primary key on [ProjectId] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [PK_Projects]
    PRIMARY KEY CLUSTERED ([ProjectId] ASC);
GO

-- Creating primary key on [GeoDataId] in table 'GeoDatas'
ALTER TABLE [dbo].[GeoDatas]
ADD CONSTRAINT [PK_GeoDatas]
    PRIMARY KEY CLUSTERED ([GeoDataId] ASC);
GO

-- Creating primary key on [UserSolutionId] in table 'UserSolutions'
ALTER TABLE [dbo].[UserSolutions]
ADD CONSTRAINT [PK_UserSolutions]
    PRIMARY KEY CLUSTERED ([UserSolutionId] ASC);
GO

-- Creating primary key on [GeoTypeId] in table 'GeoTypes'
ALTER TABLE [dbo].[GeoTypes]
ADD CONSTRAINT [PK_GeoTypes]
    PRIMARY KEY CLUSTERED ([GeoTypeId] ASC);
GO

-- Creating primary key on [PermissionId] in table 'Permissions'
ALTER TABLE [dbo].[Permissions]
ADD CONSTRAINT [PK_Permissions]
    PRIMARY KEY CLUSTERED ([PermissionId] ASC);
GO

-- Creating primary key on [BoobleDataId] in table 'BoobleDatas'
ALTER TABLE [dbo].[BoobleDatas]
ADD CONSTRAINT [PK_BoobleDatas]
    PRIMARY KEY CLUSTERED ([BoobleDataId] ASC);
GO

-- Creating primary key on [StyleId] in table 'Styles'
ALTER TABLE [dbo].[Styles]
ADD CONSTRAINT [PK_Styles]
    PRIMARY KEY CLUSTERED ([StyleId] ASC);
GO

-- Creating primary key on [UserCompanyGroup_Group_UserCompanyId], [Groups_GroupId] in table 'UserCompanyGroup'
ALTER TABLE [dbo].[UserCompanyGroup]
ADD CONSTRAINT [PK_UserCompanyGroup]
    PRIMARY KEY CLUSTERED ([UserCompanyGroup_Group_UserCompanyId], [Groups_GroupId] ASC);
GO

-- Creating primary key on [Processes_Code], [ProcessRol_Process_RolId] in table 'ProcessRol'
ALTER TABLE [dbo].[ProcessRol]
ADD CONSTRAINT [PK_ProcessRol]
    PRIMARY KEY CLUSTERED ([Processes_Code], [ProcessRol_Process_RolId] ASC);
GO

-- Creating primary key on [UserCompanies_UserCompanyId], [UserSolutions_UserSolutionId] in table 'UserCompanyUserSolution'
ALTER TABLE [dbo].[UserCompanyUserSolution]
ADD CONSTRAINT [PK_UserCompanyUserSolution]
    PRIMARY KEY CLUSTERED ([UserCompanies_UserCompanyId], [UserSolutions_UserSolutionId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [CompanyId] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [FK_CompanyGroup]
    FOREIGN KEY ([CompanyId])
    REFERENCES [dbo].[Companies]
        ([CompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyGroup'
CREATE INDEX [IX_FK_CompanyGroup]
ON [dbo].[Groups]
    ([CompanyId]);
GO

-- Creating foreign key on [UserCompanyGroup_Group_UserCompanyId] in table 'UserCompanyGroup'
ALTER TABLE [dbo].[UserCompanyGroup]
ADD CONSTRAINT [FK_UserCompanyGroup_UserCompany]
    FOREIGN KEY ([UserCompanyGroup_Group_UserCompanyId])
    REFERENCES [dbo].[UserCompanies]
        ([UserCompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Groups_GroupId] in table 'UserCompanyGroup'
ALTER TABLE [dbo].[UserCompanyGroup]
ADD CONSTRAINT [FK_UserCompanyGroup_Group]
    FOREIGN KEY ([Groups_GroupId])
    REFERENCES [dbo].[Groups]
        ([GroupId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserCompanyGroup_Group'
CREATE INDEX [IX_FK_UserCompanyGroup_Group]
ON [dbo].[UserCompanyGroup]
    ([Groups_GroupId]);
GO

-- Creating foreign key on [RolId] in table 'UserCompanies'
ALTER TABLE [dbo].[UserCompanies]
ADD CONSTRAINT [FK_RolUserCompany]
    FOREIGN KEY ([RolId])
    REFERENCES [dbo].[Rols]
        ([RolId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RolUserCompany'
CREATE INDEX [IX_FK_RolUserCompany]
ON [dbo].[UserCompanies]
    ([RolId]);
GO

-- Creating foreign key on [UserId] in table 'UserCompanies'
ALTER TABLE [dbo].[UserCompanies]
ADD CONSTRAINT [FK_UserUserCompany]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserCompany'
CREATE INDEX [IX_FK_UserUserCompany]
ON [dbo].[UserCompanies]
    ([UserId]);
GO

-- Creating foreign key on [UserCompanyId] in table 'UserPreferences'
ALTER TABLE [dbo].[UserPreferences]
ADD CONSTRAINT [FK_UserCompanyUserPreference]
    FOREIGN KEY ([UserCompanyId])
    REFERENCES [dbo].[UserCompanies]
        ([UserCompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserCompanyUserPreference'
CREATE INDEX [IX_FK_UserCompanyUserPreference]
ON [dbo].[UserPreferences]
    ([UserCompanyId]);
GO

-- Creating foreign key on [PreferenceId] in table 'UserPreferences'
ALTER TABLE [dbo].[UserPreferences]
ADD CONSTRAINT [FK_PreferenceUserPreference]
    FOREIGN KEY ([PreferenceId])
    REFERENCES [dbo].[Preferences]
        ([PreferenceId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PreferenceUserPreference'
CREATE INDEX [IX_FK_PreferenceUserPreference]
ON [dbo].[UserPreferences]
    ([PreferenceId]);
GO

-- Creating foreign key on [PreferenceId] in table 'CompanyPreferences'
ALTER TABLE [dbo].[CompanyPreferences]
ADD CONSTRAINT [FK_PreferenceCompanyPreference]
    FOREIGN KEY ([PreferenceId])
    REFERENCES [dbo].[Preferences]
        ([PreferenceId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PreferenceCompanyPreference'
CREATE INDEX [IX_FK_PreferenceCompanyPreference]
ON [dbo].[CompanyPreferences]
    ([PreferenceId]);
GO

-- Creating foreign key on [CompanyId] in table 'CompanyPreferences'
ALTER TABLE [dbo].[CompanyPreferences]
ADD CONSTRAINT [FK_CompanyCompanyPreference]
    FOREIGN KEY ([CompanyId])
    REFERENCES [dbo].[Companies]
        ([CompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyCompanyPreference'
CREATE INDEX [IX_FK_CompanyCompanyPreference]
ON [dbo].[CompanyPreferences]
    ([CompanyId]);
GO

-- Creating foreign key on [CompanyId] in table 'UserCompanies'
ALTER TABLE [dbo].[UserCompanies]
ADD CONSTRAINT [FK_CompanyUserCompany]
    FOREIGN KEY ([CompanyId])
    REFERENCES [dbo].[Companies]
        ([CompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyUserCompany'
CREATE INDEX [IX_FK_CompanyUserCompany]
ON [dbo].[UserCompanies]
    ([CompanyId]);
GO

-- Creating foreign key on [CompanyId] in table 'Rols'
ALTER TABLE [dbo].[Rols]
ADD CONSTRAINT [FK_CompanyRol]
    FOREIGN KEY ([CompanyId])
    REFERENCES [dbo].[Companies]
        ([CompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyRol'
CREATE INDEX [IX_FK_CompanyRol]
ON [dbo].[Rols]
    ([CompanyId]);
GO

-- Creating foreign key on [Processes_Code] in table 'ProcessRol'
ALTER TABLE [dbo].[ProcessRol]
ADD CONSTRAINT [FK_ProcessRol_Process]
    FOREIGN KEY ([Processes_Code])
    REFERENCES [dbo].[Processes]
        ([Code])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ProcessRol_Process_RolId] in table 'ProcessRol'
ALTER TABLE [dbo].[ProcessRol]
ADD CONSTRAINT [FK_ProcessRol_Rol]
    FOREIGN KEY ([ProcessRol_Process_RolId])
    REFERENCES [dbo].[Rols]
        ([RolId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProcessRol_Rol'
CREATE INDEX [IX_FK_ProcessRol_Rol]
ON [dbo].[ProcessRol]
    ([ProcessRol_Process_RolId]);
GO

-- Creating foreign key on [GeoTypeId] in table 'GeoDatas'
ALTER TABLE [dbo].[GeoDatas]
ADD CONSTRAINT [FK_GeoTypeGeoData]
    FOREIGN KEY ([GeoTypeId])
    REFERENCES [dbo].[GeoTypes]
        ([GeoTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GeoTypeGeoData'
CREATE INDEX [IX_FK_GeoTypeGeoData]
ON [dbo].[GeoDatas]
    ([GeoTypeId]);
GO

-- Creating foreign key on [SolutionId] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_SolutionProject]
    FOREIGN KEY ([SolutionId])
    REFERENCES [dbo].[Solutions]
        ([SolutionId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SolutionProject'
CREATE INDEX [IX_FK_SolutionProject]
ON [dbo].[Projects]
    ([SolutionId]);
GO

-- Creating foreign key on [ProjectId] in table 'GeoDatas'
ALTER TABLE [dbo].[GeoDatas]
ADD CONSTRAINT [FK_ProjectGeoData]
    FOREIGN KEY ([ProjectId])
    REFERENCES [dbo].[Projects]
        ([ProjectId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectGeoData'
CREATE INDEX [IX_FK_ProjectGeoData]
ON [dbo].[GeoDatas]
    ([ProjectId]);
GO

-- Creating foreign key on [SolutionId] in table 'UserSolutions'
ALTER TABLE [dbo].[UserSolutions]
ADD CONSTRAINT [FK_SolutionSharedSolution]
    FOREIGN KEY ([SolutionId])
    REFERENCES [dbo].[Solutions]
        ([SolutionId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SolutionSharedSolution'
CREATE INDEX [IX_FK_SolutionSharedSolution]
ON [dbo].[UserSolutions]
    ([SolutionId]);
GO

-- Creating foreign key on [PermissionId] in table 'UserSolutions'
ALTER TABLE [dbo].[UserSolutions]
ADD CONSTRAINT [FK_PermissionSharedSolution]
    FOREIGN KEY ([PermissionId])
    REFERENCES [dbo].[Permissions]
        ([PermissionId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PermissionSharedSolution'
CREATE INDEX [IX_FK_PermissionSharedSolution]
ON [dbo].[UserSolutions]
    ([PermissionId]);
GO

-- Creating foreign key on [UserCompanies_UserCompanyId] in table 'UserCompanyUserSolution'
ALTER TABLE [dbo].[UserCompanyUserSolution]
ADD CONSTRAINT [FK_UserCompanyUserSolution_UserCompany]
    FOREIGN KEY ([UserCompanies_UserCompanyId])
    REFERENCES [dbo].[UserCompanies]
        ([UserCompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserSolutions_UserSolutionId] in table 'UserCompanyUserSolution'
ALTER TABLE [dbo].[UserCompanyUserSolution]
ADD CONSTRAINT [FK_UserCompanyUserSolution_UserSolution]
    FOREIGN KEY ([UserSolutions_UserSolutionId])
    REFERENCES [dbo].[UserSolutions]
        ([UserSolutionId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserCompanyUserSolution_UserSolution'
CREATE INDEX [IX_FK_UserCompanyUserSolution_UserSolution]
ON [dbo].[UserCompanyUserSolution]
    ([UserSolutions_UserSolutionId]);
GO

-- Creating foreign key on [GeoDataId] in table 'BoobleDatas'
ALTER TABLE [dbo].[BoobleDatas]
ADD CONSTRAINT [FK_GeoDataBoobleData]
    FOREIGN KEY ([GeoDataId])
    REFERENCES [dbo].[GeoDatas]
        ([GeoDataId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GeoDataBoobleData'
CREATE INDEX [IX_FK_GeoDataBoobleData]
ON [dbo].[BoobleDatas]
    ([GeoDataId]);
GO

-- Creating foreign key on [StyleId] in table 'GeoDatas'
ALTER TABLE [dbo].[GeoDatas]
ADD CONSTRAINT [FK_StyleGeoData]
    FOREIGN KEY ([StyleId])
    REFERENCES [dbo].[Styles]
        ([StyleId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StyleGeoData'
CREATE INDEX [IX_FK_StyleGeoData]
ON [dbo].[GeoDatas]
    ([StyleId]);
GO

-- Creating foreign key on [GroupCode] in table 'Processes'
ALTER TABLE [dbo].[Processes]
ADD CONSTRAINT [FK_ProcessGroupProcess]
    FOREIGN KEY ([GroupCode])
    REFERENCES [dbo].[ProcessGroups]
        ([Code])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProcessGroupProcess'
CREATE INDEX [IX_FK_ProcessGroupProcess]
ON [dbo].[Processes]
    ([GroupCode]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------